#!/usr/bin/python
# coding: utf-8
# VIOLATED
# 2012

import decimal
import pygame
from pygame.locals import *

def ctime():
    clock.tick(30)
    return clock.get_fps()

class Resolution:
    def __init__(self):
        pygame.init()
        pygame.display.set_caption('[ViolaTed] [Menu]')
        self.X = 1000
        self.Y = 750
        self.arrowY = 426
        self.window = pygame.display.set_mode((self.X, self.Y))
        self.back = pygame.image.load("graph/resolution.png").convert()        
        self.arrow = pygame.image.load("graph/arrow.png").convert_alpha()
        self.choose()

    def choose(self):
        self.next = True
        while self.next:
            self.window.blit(self.back, (0, 0))
            self.window.blit(self.arrow, (50, self.arrowY))
            pygame.display.flip()
            for event in pygame.event.get():
                if event.type == QUIT or pygame.key.get_pressed()[K_ESCAPE]:
                    pygame.display.quit()
                    exit()
                if pygame.key.get_pressed()[K_DOWN]:
                    self.arrowY = 560
                if pygame.key.get_pressed()[K_UP]:
                    self.arrowY = 426
                if pygame.key.get_pressed()[K_RETURN]:
                    if self.arrowY == 426:
                        self.id = 0
                    elif self.arrowY == 560:
                        self.id = 1
                    self.next = False

class Window:
    def __init__(self, full):
        pygame.display.set_caption('[ViolaTed] [prototype]')
        pygame.key.set_repeat(100, 30)
        self.X = 1000
        self.Y = 750
        if (full == 1):
            self.window = pygame.display.set_mode((self.X, self.Y), HWSURFACE | FULLSCREEN)
        elif (full == 0):
            self.window = pygame.display.set_mode((self.X, self.Y))

    def text(self):
        pygame.draw.rect(self.back, (0, 0, 0), (0, 0, self.X, 73))
        self.lives = font.render(str(hero.lives) + " lives", 1, ((255, 255, 255)))
        self.hits = font.render(str(hero.hits) + "/5 hits", 1, ((255, 255, 255)))
        self.tfps = font.render(str(round(fps, 1)) + " fps", 1, ((255, 255, 255)))
        self.score = font.render(str(hero.score) + " pts", 1, (255, 255, 255))
        self.lol = font_title.render("DEMO !", 1, (255, 255, 255))
        self.back.blit(self.lives, (10, 10))
        self.back.blit(self.hits, (10, 40))
        self.back.blit(self.tfps, (self.X - 175, 10))
        self.back.blit(self.score, (self.X - 192, 40))
        self.back.blit(self.lol, (400, 13))
    
    def background(self):
        self.window.blit(self.back, (0, 0))

class Player:
    def __init__(self):
        self.X = 30
        self.Y = 422
        self.lives = 5
        self.hits = 0
        self.score = 10000
        self.img = pygame.image.load("graph/perso.png").convert_alpha()

    def show(self):
        win.window.blit(self.img, (self.X, self.Y))

    def moves(self):
        if pygame.key.get_pressed()[K_RIGHT]:
            self.img = pygame.image.load("graph/perso.png").convert_alpha()
            if self.X + 5 < win.X - self.img.get_width():
                self.X += 5
        if pygame.key.get_pressed()[K_RIGHT] and pygame.key.get_pressed()[K_LSHIFT]:
            self.player = pygame.image.load("graph/perso.png").convert_alpha()
            if self.X + 13 < win.X - self.img.get_width():
                self.X += 13
        if pygame.key.get_pressed()[K_LEFT]:
            self.img = pygame.image.load("graph/perso2.png").convert_alpha()
            if (self.X - 5) > 0:
                self.X -= 5
        if pygame.key.get_pressed()[K_LEFT] and pygame.key.get_pressed()[K_LSHIFT]:
            self.img = pygame.image.load("graph/perso2.png").convert_alpha()
            if (self.X - 13) > 0:
                self.X -= 13
        if pygame.key.get_pressed()[K_SPACE]:
            i = 0
            while i <= 10:
                self.Y -= i
                i += 1
                win.window.blit(win.back, (0, 0))
                win.window.blit(hero.img, (self.X, self.Y))
                pygame.display.flip()
            #if linux
            pygame.time.delay(100)
            while self.Y < 422:
                self.Y += 5

class Levels:
    def __init__(self, nb):
        self.id = 0
        if nb == 0:
            self.intro()
        if nb == 1:
            self.one()
            
    def intro(self):
        music.level(self.id)
        self.title = pygame.image.load("graph/title.png").convert()
        win.window.blit(self.title, (0, 0))
        pygame.display.flip()
        pygame.time.delay(5500)
        pygame.mixer.music.load("music/intro.ogg")
        pygame.mixer.music.play()
        i = 1
        while i < 20:
            self.title = pygame.image.load("graph/intro/"+str(i)+".png").convert()
            win.window.blit(self.title, (0, 0))
            pygame.display.flip()
            pygame.time.delay(2200)
            i += 1
    
    def one(self):
        self.id = 1
        stage.select(self.id)
        music.level(self.id)
        background.select(self.id)
        win.text()
        pygame.display.flip()

class Backgrounds():
    def __init__(self):
        self.nothing = False
        
    def select(self, id):
        win.back = pygame.image.load("graph/back"+str(id-1)+".png").convert()

class Stage:
    def __init__(self):
        self.nothing = False 
       
    def select(self, id):
        self.next = True
        pygame.mixer.music.load("music/stage.ogg")
        pygame.mixer.music.play()
        self.title = pygame.image.load("graph/stage"+str(id-1)+".png").convert()
        win.window.blit(self.title, (0, 0))
        pygame.display.flip()
        pygame.time.delay(7000)

class Music(Levels):
    def __init__(self):
        self.music = True
    
    def level(self, id):
        if id == 0:
            #pygame.mixer.music.load("music/gamestart.ogg")            
        elif id == 1:
            #pygame.mixer.music.load("music/scienceisfun.ogg")
        pygame.mixer.music.play(-1)
    #pygame.mixer.music.set_volume(0.1)

    def pause(self):
        if pygame.key.get_pressed()[K_m] and self.music == True:
            pygame.mixer.music.pause()
            self.music = False
        elif pygame.key.get_pressed()[K_m] and self.music == False:
            pygame.mixer.music.unpause()
            self.music = True

class PauseGame:
    def __init__(self):
        if pygame.key.get_pressed()[K_ESCAPE]:
            self.next = True
            self.arrowY = 290
            self.musicVolume = pygame.mixer.music.get_volume()
            if self.musicVolume >= 0.3:
                pygame.mixer.music.set_volume(0.2)
            win.back = pygame.image.load("graph/pause.png").convert_alpha()
            win.arrow = pygame.image.load("graph/arrow.png").convert_alpha()
            while self.next:
                win.window.blit(win.back, (0, 0))
                win.window.blit(win.arrow, (50, self.arrowY))
                pygame.display.flip()
                for event in pygame.event.get():
                    QuitGame()
                    if pygame.key.get_pressed()[K_ESCAPE]:
                        pygame.mixer.music.set_volume(self.musicVolume)
                        background.select(level.id)
                        self.next = False
                    if pygame.key.get_pressed()[K_DOWN]:
                        if self.arrowY < 125*3+290:
                            self.arrowY += 125
                    if pygame.key.get_pressed()[K_UP]:
                        if self.arrowY > 290:
                            self.arrowY -= 125
                    if pygame.key.get_pressed()[K_RETURN]:
                        if self.arrowY == 290:
                            pygame.mixer.music.set_volume(self.musicVolume)
                            background.select(level.id)
                            self.next = False
                        if self.arrowY == 125*1+290:
                            self.music()
                        if self.arrowY == 125*2+290:
                            self.controls()
                        if self.arrowY == 125*3+290:
                            pygame.display.quit()
                            exit()

    def music(self):
        next_music = True
        pygame.mixer.music.set_volume(self.musicVolume)
        win.back = pygame.image.load("graph/music.png").convert_alpha()
        win.volume = pygame.image.load("graph/volume.png").convert_alpha()
        i = 0.1
        j = 104
        while next_music:
            self.musicVolume = round(self.musicVolume,1)
            win.window.blit(win.back, (0,0))
            if self.musicVolume > 0.0 and self.musicVolume <= 1.0:
                while i <= self.musicVolume :#- 0.1:
                    i += 0.1
                    j += 68
                    win.window.blit(win.volume, (j, 463))
                    pygame.display.flip()
            else:
                self.musicVolume = 0
                pygame.display.flip()
            for event in pygame.event.get():
                print self.musicVolume
                QuitGame()
                if pygame.key.get_pressed()[K_LEFT]:
                    if self.musicVolume >= 0.0:
                        self.musicVolume -= 0.1
                        i = 0.0
                        j = 104
                        pygame.mixer.music.set_volume(self.musicVolume)
                    elif self.musicVolume < 0.0:
                        self.musicVolume = 0
                        i = 0.0
                        j = 104
                if pygame.key.get_pressed()[K_RIGHT]:
                    if self.musicVolume < 1:
                        self.musicVolume += 0.1
                        pygame.mixer.music.set_volume(self.musicVolume)
                        i = 0.0
                        j = 104
                if pygame.key.get_pressed()[K_RETURN]:
                        win.back = pygame.image.load("graph/pause.png").convert_alpha()
                        next_music = False

    def controls(self):        
        print ("controls options not implemented yet!")

class QuitGame:
    def __init__(self):
        if event.type == QUIT:
            pygame.display.quit()
            exit()

res = Resolution()
win = Window(res.id)
font = pygame.font.Font('emulogic.ttf', 20)
font_title = pygame.font.Font('emulogic.ttf', 40)
clock = pygame.time.Clock()
fps = clock.get_fps()
hero = Player()
music = Music()
background = Backgrounds()
stage = Stage()
level = Levels(0)
level = Levels(1)

next = True
while next:
    fps = ctime()
    for event in pygame.event.get():
        PauseGame()
        QuitGame()
        music.pause()
        hero.moves()
    win.background()
    win.text()
    hero.show()
    pygame.display.flip()

if level.id == 2:
    print "level 2 !"
else:
    pygame.display.quit()
    exit()
